package com.mvc.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.common.utils.StringUtil;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.mvc.model.Message;
import com.mvc.model.User;
import com.mvc.model.Venue;
import com.mvc.request.ReqPage;
import com.mvc.request.ReqVenueMessagePage;
import com.mvc.request.ReqSendMessage;
import com.mvc.request.ReturnData;
import com.mvc.response.RspMessage;
import com.mvc.response.RspPage;
import com.mvc.service.MessageService;

public class MessageServiceImpl implements MessageService {

	/**
	 * 场馆发布留言
	 * 
	 * @param message
	 * @return
	 */
	public ReturnData<Boolean> create(ReqSendMessage req) {

		if (req == null) {
			return new ReturnData<Boolean>(false, 500, "请求参数不完整!");
		}
		if (req.getUserId() <= 0) {
			return new ReturnData<Boolean>(false, 202, "请登录!");
		}

		User user = User.dao.findById(req.getUserId());
		if (user == null) {
			return new ReturnData<Boolean>(false, 202, "当前用户不存在,请登录!");
		}
		// 3 判断用户mac 与 数据库mac是否相同返回都是 错误400
		if (user.getInt(User.TableField.TYPE) != 3 && !req.getFinalData().getMac().equals(user.getStr("mac"))) {
			return new ReturnData<Boolean>(false, 400, "当前用户已在其他设备登录，请重新登录!");
		}
		
		if (StringUtil.isEmpty(req.getContent()))
		{
			return new ReturnData<Boolean>(false, 202, "请输入留言信息!");
		}

		Message message = new Message();
		message.set(Message.TableField.VENUEID, req.getVenueId());
		message.set(Message.TableField.CONTENT, req.getContent());
		if (10000  == req.getUserId() )
		{
			// 游客-自动产生游客名称
		    String	  name ="游客" + (int)((Math.random()*9+1)*10000);
			message.set(Message.TableField.NAME, name);
		}
		
		message.set(Message.TableField.USERID, req.getUserId());
		message.set(Message.TableField.CREATETIME, new Date());
		if (message.save())
		{
			Venue venue = Venue.dao.findById( req.getVenueId());
			if (venue != null) {
				// 更新怀念币数量
				int money = venue.getInt(Venue.TableField.MONEY);
				// 更新祭拜数量
				int worshipSize = venue.getInt(Venue.TableField.WORSHIPSIZE) + 1;
				int grade = venue.getInt(Venue.TableField.GRADE);

				// 　达到某个等级所需总经验=(等级+1)*等级*10
				if ((money + worshipSize) >= Math.abs((grade + 1) * grade * 10)) {
					grade += 1;
				}

				venue.set(Venue.TableField.WORSHIPSIZE, worshipSize);
				venue.set(Venue.TableField.MONEY, money);
				venue.set(Venue.TableField.GRADE, grade);
				venue.update();

			}
			return new ReturnData<Boolean>(true, 200, "留言成功!");
		}else
		{
			return new ReturnData<Boolean>(false, 201, "留言失败!");
		}
	}

	/***
	 * 获取最新的留言记录【分页】
	 * 
	 * @return
	 */
	public ReturnData<RspPage<RspMessage>> queryNewPage(ReqPage req) {
		Page<Message> page = Message.dao.getNewPage(req.getNowPage(), req.getPageSize());
		RspPage<RspMessage> rspPage = new RspPage<RspMessage>();
		rspPage.setNowPage(req.getNowPage());
		rspPage.setTotalPage(page.getTotalPage());
		rspPage.setList(getRspMessages(page.getList()));
		return new ReturnData<RspPage<RspMessage>>(rspPage,200,"OK");
	}

	/***
	 * 获取场馆留言记录【分页】
	 * 
	 * @return
	 */
	public ReturnData<RspPage<RspMessage>> queryVenuePage(
			ReqVenueMessagePage req) {
		Page<Message> page = Message.dao.getVenuePage(req.getNowPage(), req.getPageSize(),req.getVenueId());
		RspPage<RspMessage> rspPage = new RspPage<RspMessage>();
		rspPage.setNowPage(req.getNowPage());
		rspPage.setTotalPage(page.getTotalPage());
		rspPage.setList(getRspMessages(page.getList()));
		return new ReturnData<RspPage<RspMessage>>(rspPage,200,"OK");
	}
	
	private List<RspMessage> getRspMessages(List<Message> data)
	{
		List<RspMessage> list = new ArrayList<>();
		for (Message msg : data)
		{
		
			RspMessage rspMessage = new RspMessage();
			
			rspMessage.setId(msg.getInt(Message.TableField.ID));
			rspMessage.setContent(msg.getStr(Message.TableField.CONTENT));
			rspMessage.setCreateTime(msg.getDate(Message.TableField.CREATETIME));
			
			User user = msg.getUser();
			if (user != null)
			{
				int type = 	user.getInt(User.TableField.TYPE);
				String name = user.getStr(User.TableField.NAME);
				
				if (type ==3)
				{
					//游客-名称从留言里面获取
					String tempName =msg.getStr(Message.TableField.NAME);
					if (StrKit.notBlank(tempName))
					{
						name = tempName;
					}
				}
				rspMessage.setUserHead(user.getStr(User.TableField.HEAD));
				rspMessage.setUserId(user.getInt(User.TableField.ID));
				rspMessage.setUserName(name);
			}
			
			
			Venue venue = msg.getVenue();
			if (venue != null)
			{
				rspMessage.setVenueId(venue.getInt(Venue.TableField.ID));
				rspMessage.setVenueName(venue.getStr(Venue.TableField.NAME));
				list.add(rspMessage);
			}
			
		}
		return list;
	}

}
